from django.db import models

class Brand(models.Model):
    brand_name = models.CharField(max_length=20,verbose_name='品牌',unique=True)

    def __str__(self):
        return self.brand_name


class Category(models.Model):

    name = models.CharField(max_length=500, verbose_name='分类名称')
    # sort = models.IntegerField(default=0, verbose_name='排序值')
    parent = models.ForeignKey('self', default=0, null=True, blank=True, verbose_name='上级分类',on_delete=models.CASCADE)
    is_root = models.BooleanField(default=False, verbose_name='是否是一级分类')

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Size(models.Model):

    size_kind = models.CharField(max_length=5)

    def __str__(self):
        return self.size_kind

class Color(models.Model):

    color_name = models.CharField(max_length=5,verbose_name='颜色')

    def __str__(self):
        return self.color_name


class Goods (models.Model):
    """
    商品详细信息
    """
    SEASON_CHOICE = (
        ('S', '春季'),
        ('M', '夏季'),
        ('A', '秋季'),
        ('W', '冬季')
    )

    brand = models.ForeignKey (Brand, on_delete=models.CASCADE)  # 商品品牌
    category = models.ManyToManyField(Category)  # 商品分类
    good_name = models.CharField (max_length=50, verbose_name='商品名称', blank=True)  # 商品名字
    goods_text = models.CharField (max_length=20, verbose_name='商品描述', blank=True)  # 商品描述
    price = models.DecimalField (max_digits=6, decimal_places=2, verbose_name='价格', blank=True,null=True)  # 商品价格
    sales_count = models.IntegerField (default=0, verbose_name='售出数量', blank=True)  # 售出量
    has_invoice = models.BooleanField (default=0, verbose_name='提供发票', blank=True)  # 是否提供发票
    keywords = models.CharField (max_length=200, verbose_name='关键词', blank=True)  # 商品关键词
    status = models.BooleanField (default=0, verbose_name='状态')  # 商品是否正在上架
    discount = models.FloatField(default = 0,verbose_name='折扣') #折扣数
    season = models.CharField (max_length=2, choices=SEASON_CHOICE, blank=True,verbose_name='季节')

    def __str__ (self):
        return self.good_name

class GoodsImage(models.Model):
    goods = models.ForeignKey(Goods,on_delete=models.CASCADE)
    image = models.ImageField(upload_to='goods/img')


class Ckeckcondition (models.Model):
    size = models.ForeignKey(Size, null=True, blank=True, on_delete=models.SET_NULL)  # 商品有哪些尺码
    color = models.ForeignKey (Color, on_delete=models.CASCADE)
    goods = models.ForeignKey (Goods,on_delete=models.CASCADE)
    size_name = models.CharField (max_length=20, verbose_name='尺码大小')
    good_count = models.IntegerField (default=0, verbose_name='库存', blank=True)

    def __str__ (self):
        return self.size_name